//
//  DataController.m
//  TestNumber
//
//  Created by Xuan Mai on 3/18/14.
//  Copyright (c) 2014 Xuan Mai. All rights reserved.
//

#import "DataController.h"
#import "Global.h"
#define PassAccount @"Pass"
#define FailAccount @"Fail"
@implementation DataController

+ (int)getSum:(int)value{
    return (value / 10 + value % 10);
}

+ (int)getNumberWithString:(NSString *)strNumber{
    NSString * temp;
    int number = 0;
    int x = 0;
    for (int i = 0; i < strNumber.length; i++){
        temp = [strNumber substringWithRange:NSMakeRange(i, 1)];
        x = [temp intValue] * (i % 2 + 1);
        number += [self getSum:x];
    }
    return number;
}

+ (BOOL)checkAccount:(NSString *)strNumber{
    int checkNumber = [self getNumberWithString:strNumber];
    return ((checkNumber % 10) == 0);
}

+ (NSString *)getResultWithAccount:(NSString *)strNumber{
    BOOL check = [self checkAccount:strNumber];
    return (check ? PassAccount : FailAccount);

}

+ (NSString *)convertDateToString:(NSDate *)date{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    // this is imporant - we set our input date format to match our input string
    // if format doesn't match you'll get nil from your string, so be careful
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
    [dateFormatter setDateFormat:FORMATDATE];
    NSString *strDate = [dateFormatter stringFromDate:date];
    return strDate;
}
@end
